import Vue from 'vue'
import VueRouter from 'vue-router'
import Center from '@/views/Center'
import Search from '@/views/Search'
import Films from '@/views/Films'
import Comingsoon from '@/views/films/Comingsoon'
import Nowplaying from '@/views/films/Nowplaying'
import Cinemas from '@/views/Cinemas'
import Detail from '@/views/Detail'
import City from '@/views/City'

Vue.use(VueRouter)

const routes = [
  {
    path: '/center',
    component: Center,
    meta: {
      isKerwinRequired: true
    }
  },
  {
    path: '/order',
    component: () => import('@/views/Order'),
    meta: {
      isKerwinRequired: true
    }
  },
  {
    path: '/city',
    component: City
  },
  {
    path: '/cinemas',
    component: Cinemas
  },
  {
    path: '/films',
    component: Films,
    children: [
      {
        path: '/films/nowplaying',
        component: Nowplaying
      },
      {
        path: '/films/comingsoon',
        component: Comingsoon
      },
      {
        path: '/films',
        redirect: '/films/nowplaying'
      }
    ]
  },
  {
    name: 'kerwinPlay',
    path: '/datail/:id',
    component: Detail
  },
  {
    path: '/search',
    component: Search
  },
  {
    path: '/login',
    component: () => import('@/views/Login')
  },
  {
    path: '*',
    redirect: '/films'
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.isKerwinRequired) {
    if (localStorage.getItem('token')) {
      next()
    } else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }
}

)
export default router
